<template>
    <div class="comment">
        <h3>评论区</h3>
        <textarea placeholder="请输入内容" maxlength="120" rows="3"></textarea>
        <mt-button type="primary" size="large">发表评论</mt-button>

        <div class="cmt-list">
            <div class="cmt-item" v-for="(comment,i) in comments" :key="i">
                <div class="cmt-title">
                    <i>{{ i+1 }}楼&nbsp;&nbsp;{{ comment.user_name }}</i>
                    <span>发表时间：{{ comment.add_time | dateFormat }}</span>
                </div>
                <div class="cmt-body">
                    {{ comment.content == ""?'此用户就点了下回复，没有评论内容！':comment.content }}
                </div>
            </div>
        </div>

        <mt-button type="danger" size="large" plain @click="loadMore">加载更多</mt-button>
    </div>
</template>

<script>
    import {Toast} from 'mint-ui'

    export default {
        data() {
            return {
                pageIndex: 1,
                comments: []
            }
        },
        created() {
            this.getComments()
        },
        methods: {
            getComments() { //获取评论
                this.$http.get('api/getcomments/' + this.id + '?pageindex=' + this.pageIndex)
                    .then(res => {
                        if (res.body.status === 0) {
                            this.comments = this.comments.concat(res.body.message)
                        } else {
                            Toast('获取评论失败')
                        }
                    })
            },
            loadMore() { //读取更多评论
                this.pageIndex++
                this.getComments()
            }
        },
        props: {
            id: {
                type: String,
                default: 0
            },
        },
    }
</script>

<style scoped lang="scss">
    .comment {
        padding: 5px;

        h3 {
            text-align: center;
        }

        textarea {
            margin-bottom: 0;
        }

        button {
            &:nth-of-type(2) {
                margin-top: 5px;
            }
        }


        .cmt-list {
            margin: 5px 0;

            .cmt-item {
                font-size: 13px;
                margin: 1px 0;

                .cmt-title {
                    line-height: 30px;
                    background-color: #ccc;
                    display: flex;
                    justify-content: space-between;
                    padding: 0 2px;
                }

                .cmt-body {
                    line-height: 35px;
                    text-indent: 2em;
                }
            }
        }
    }
</style>